package com.happyfamily.springboot.crowdsourcingplatform.service.pan.impl;

import com.happyfamily.springboot.crowdsourcingplatform.base.result.Results;
import com.happyfamily.springboot.crowdsourcingplatform.dao.weng.RoleUserDao;
import com.happyfamily.springboot.crowdsourcingplatform.dao.pan.UserDao;
import com.happyfamily.springboot.crowdsourcingplatform.dto.NeedDto;
import com.happyfamily.springboot.crowdsourcingplatform.model.Need;
import com.happyfamily.springboot.crowdsourcingplatform.model.ServerType;
import com.happyfamily.springboot.crowdsourcingplatform.model.SysRoleUser;
import com.happyfamily.springboot.crowdsourcingplatform.model.SysUser;
import com.happyfamily.springboot.crowdsourcingplatform.service.pan.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.lang.reflect.Type;
import java.util.List;

/**
 * @Author Administrator
 * @create 2020/02/15 23:05
 */
@Service
public class UserServiceImpl implements UserService
{
	@Autowired(required = false)
	private UserDao userDao;

	@Autowired(required = false)
	private RoleUserDao roleUserDao;

	/**
	 * 保存用户信息
	 * @param user
	 * @param roleId
	 * @return
	 */
	@Override
	public Results save(SysUser user, Integer roleId,Integer sid)
	{
		if (roleId !=null){
			//保存user
			//判断是否为非服务商
			if (roleId == 3){
				user.setSid(1);
			}else{
				user.setSid(sid);
			}
			userDao.save(user);
			//保存roleUser
			SysRoleUser sysRoleUser=new SysRoleUser();
			sysRoleUser.setRoleId(roleId);
			sysRoleUser.setUserId(user.getId().intValue());
			roleUserDao.save(sysRoleUser);
			return  Results.success();
		}
		return  Results.failure();
	}


	@Override
	public SysUser getUserByPhone(String uphone)
	{

	 return userDao.getUserByPhone(uphone);
	}

	@Override
	public SysUser getUser(String uaccount)
	{
		return userDao.getUser(uaccount);
	}

	@Override
	public Results<SysUser> getAllEmployers(Integer startPosition, Integer limit)
	{
		return Results.success(userDao.getSum(), userDao.getList(startPosition, limit));
	}

	@Override
	public String resetPassword(Long uid)
	{
		String str = "";
		int i=userDao.resetPassword(uid);
		if (i > 0)
		{
			str = "yes";
		} else
		{
			str = "no";
		}
		return str;
	}

	@Override
	public String changeStates(Long uid, Long pid)
	{
		String str = "";
		if (pid==1){
			pid=Long.valueOf(2);
		}else if (pid==2){
			pid=Long.valueOf(1);
		}else{
			pid=Long.valueOf(3);
		}
		int i=userDao.changeStates(uid,pid);
		if (i > 0)
		{
			str = "yes";
		} else
		{
			str = "no";
		}
		return str;
	}

	@Override
	public Results<SysUser> getSpecifyEmployers(String uname, Integer startPosition, Integer limit)
	{
		return Results.success(userDao.getSpecifySum(uname), userDao.getSpecifyList(uname,startPosition, limit));
	}

	@Override
	public String getEmployersStates(Long pid)
	{

		return userDao.getEmployersStates(pid);
	}

	@Override
	public Results<SysUser> getServiceProvidersList(Integer startPosition, Integer limit)
	{
		return Results.success(userDao.getServiceProvidersSum(2,7), userDao.getServiceProvidersList(2,7,startPosition, limit));
	}

	@Override
	public String getServiceProvidersStates(Integer id)
	{
		return userDao.getServiceProvidersStates(id);
	}


	@Override
	public List<ServerType> findById(long sparentid)
	{
		return userDao.getById(sparentid);
	}

	@Override
	public List<ServerType> Status(long sid)
	{
		return userDao.getStatus(sid);
	}





	@Override
	public Results savePublishing(Need need,String nuseraccount)
	{

		//根据sid找到对应的服务类型
		String  sname=userDao.getSnameBySid(need.getSname());

		need.setSname(sname);
		int  i=userDao.saveUserPublishing(need,nuseraccount);
		if (i>0){
			return Results.success();
		}
		return Results.failure()  ;
	}





	@Override
	public List<SysUser> findByRid(Integer i)
	{
		return userDao.getByRid(i);
	}


}
